import React from 'react';
import { connect } from 'react-redux';
import { Redirect, Switch, Route } from 'react-router-dom';
import { object } from 'prop-types';

import AsyncLoad from '../component/common/AsyncLoad';

const AsyncLogin = AsyncLoad({ loader: () => import('../pages/User/Login') })
const AsyncRegister = AsyncLoad({ loader: () => import('../pages/User/Register') })
function Auth({ logged, location }) {
    const { from, params } = location.state || { from: { pathname: '/' } };
    return (
        <div>
            auth
            {logged ? (
                <Redirect to={from} />
            ) : (
                    <Switch>
                        <Route path='/auth/login' component={AsyncLogin} />
                        <Route path='/auth/register' component={AsyncRegister} />
                    </Switch>
                )}
        </div>
    )
}
const propTypes = {
    location: object.isRequired
}
const mapStateToProps = state => ({
    logged: state.user.logged
})

Auth.propTypes = propTypes
export default connect(mapStateToProps)(Auth);